Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Datatypes

Complex datatype

In Python, the complex data type is used to represent a number with both a real and an imaginary component. Complex numbers are written in the form a + bj, where a is the real part and b is the imaginary part. Here’s how you can declare a complex number in Python:
Declaring complex numer example in python # Declare a complex number num = 3 + 4j print(num) print(type(num))

Output

(3+4j) <class 'complex'>
In this example, 3 is the real part and 4 is the imaginary part of the complex number. You can also use the complex() function to create a complex number:
Declaring complex numer using complex() function in python # Declare a complex number using the complex() function num = complex(3, 4) print(num)

Output

(3+4j)
In this example, the first argument to the complex() function is the real part and the second argument is the imaginary part. You can access the real and imaginary parts of a complex number using the real and imag attributes, respectively:
Access the real and imag parts of a complex number using the real and imag attributes in python # Access the real and imaginary parts num = 3 + 4j print(num.real) print(num.imag)

Output

3.0 4.0
In these examples, num.real returns the real part of the complex number num, and num.imag returns the imaginary part. Complex numbers are used in many fields, including engineering, physics, and mathematics. In Python, they can be used in calculations just like integers and floats. However, it’s important to note that not all operations that work with integers and floats also work with complex numbers. For example, you cannot use the > or < operators to compare two complex numbers.

  📌TAGS

★python ★ datatypes ★ complex

Tutorials